﻿//Copyright (C) <2013>  <jonathan cleeve norton> All Rights Reserved 
//Contact jon.norton@fin-plus.co.uk website <http://www.fin-plus.co.uk/>
using System;
using System.Windows;
using FinPlusCommon;
using u = FinPlusUtility.Utility;
using log = Logger.Logger;

namespace FinPlusWPFControls
{
    //Note Prime use edit template columns use primative controls for data template
    public partial class FinPlusValueFunction : FinPlusControl
    {
        public override string[] TestStates { get { return new string[] { "TestString", "ViewString", "IsValid" }; } }
        public override string TestString { get { return string.Empty; } }

        public string ViewString { get { return  string.Empty; } }
        public string Function { get { return GetValue(FunctiontProperty).ToString(); } set { SetValue(FunctiontProperty, value); } }
        public new bool IsEnabled { get { return (bool)GetValue(IsEnabledProperty); } set { } }

        public static readonly DependencyProperty FunctiontProperty = DependencyProperty.Register(Propertys.Function.ToString(), typeof(string), typeof(FinPlusValueFunction), new UIPropertyMetadata(Callback));
        public new static readonly DependencyProperty IsEnabledProperty = DependencyProperty.Register(Propertys.IsEnabled.ToString(), typeof(bool), typeof(FinPlusValueFunction));

        private bool _hasSetEnable;
        private enum Propertys { Function, IsEnabled }

        //construct
        public FinPlusValueFunction()
        {
            InitializeComponent();
            IsValid = true;
        }

        //common control interface
        public override void CmdExecute(IFunction command)
        {
            try
            {
                CmdCapture(command);

                switch ((Cmds)Enum.Parse(typeof(Cmds), command.Name, true))
                {
                    case Cmds.Open: Open(); break;
                    default: throw new Exception(string.Format("event not recognised {0}", command.Name));
                }
            }
            catch (CmdExecuteException e)
            {
                IsValid = log.Log(Logger.Level.Warning, Id, Config, e.Message, e);
            }
        }

        //public
        public void SetControl()
        {
            //permission
            if (!_hasSetEnable)
            {
                 button.IsEnabled = IsEnabled;
                _hasSetEnable = true;
            }
        }

        //private
        private void Open()
        {
            FinPlusWindow.OpenEditor(new Function((object[,]) u.StringToArray2d(Function)), "FuncEdit", Env);
        }

        //cmds
        private enum Cmds { Open }

        //events
        private static void Callback(DependencyObject d, DependencyPropertyChangedEventArgs a) { ((FinPlusValueFunction)d).SetControl(); }
    }
}